﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VQPMERKM
    {
        public readonly static VQPMERKM Instance = new VQPMERKM();

        private string _MERKM; public string MERKM { get { return _MERKM; } set { _MERKM = value; OnPropertyChanged("MERKM", value); } }
        private string _NORM; public string NORM { get { return _NORM; } set { _NORM = value; OnPropertyChanged("NORM", value); } }
        public string QT = "";
        public void NEWQT()
        { 
            TBMain.Clear();
            dwData = TBMain.NewRow();
            SetUpNewRow(ref dwData);
            dwData["QPMMERKM"] = ART;
            dwData["QPMNORM"] = NORM;
            Insert(dwData);
            ART = "";
        }
        public void COPYQT(DataRow _dwQP)
        {
            if (_dwQP == null)
                return;
            if (ART.Trim().Length == 0)
            {
                return;
            }
            DataRow dwTemp = TBMain.Clone().NewRow();
            dwTemp.ItemArray = _dwQP.ItemArray;
            TBMain.Clear();
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwTemp.ItemArray; 
            dwData["QPMMERKM"] = ART;
            Insert(dwData);
        }
        public void Insert(DataRow _dwQP)
        {
            if (_dwQP == null)
                return;
            dwData["QPMNEUB"] = app.User;
            dwData["QPMNEUD"] = date;
            InsertQPMERKM(_dwQP, app.company);
            TBMain.Rows.InsertAt(_dwQP, 0);
            ART = "";
        }
        public void SAVEQP(DataRow _dwQP)
        {
            if (_dwQP == null)
                return;
            dwData["QPMAENB"] = app.User;
            dwData["QPMAEND"] = date;
            UpdateQPMERKM(_dwQP, app.company, 0);
        }
        public string TTLQT()
        {
            QT = "";
            if (QT.Length > 0)
                return QT;
            dwData = TBMain.NewRow(); 
            dwData["QPMMANDANT"] = app.Mandant;
            SResult = QueryQPMERKM(dwData, -2);
            string FD = "";
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                FD = row["QPMMERKM"].ToString().Trim();
               // QT += "MAX(DECODE(QOEMERKM,'" + row["QPMMERKM"].ToString() + "',QOENSWERT,0)) AS " + row["QPMMERKM"].ToString().Trim() + ",";
             //   QT += "DECODE(RTRIM(QOEMERKM),'" + FD + "',QOENSWERT,0) AS " + FD.Trim() + ",";
                QT += " NVL((SELECT TRIM(QOEASWERT) AS QOEASWERT FROM QOSTEP Q1 WHERE Q1.QOEMANDANT = Q.QOKMANDANT AND Q1.QOEORDER = Q.QOKORDER AND RTRIM(Q1.QOEMERKM) = '" + FD.Trim() + "'),'0') AS " + FD.Trim() + ",";
             }
            QT += "****";
            QT = QT.Replace(",****", ",");
            if (QT == "****")
                QT = "";
            return QT;
        }
        public string BXQT()
        {
            QT = "";
            if (QT.Length > 0)
                return QT;
            dwData = TBMain.NewRow();
            dwData["QPMMANDANT"] = app.Mandant;
            SResult = QueryQPMERKM(dwData, -3);
            string FD = "";
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                FD = row["QPMMERKM"].ToString().Trim();
                // QT += "MAX(DECODE(QOEMERKM,'" + row["QPMMERKM"].ToString() + "',QOENSWERT,0)) AS " + row["QPMMERKM"].ToString().Trim() + ",";
                //   QT += "DECODE(RTRIM(QOEMERKM),'" + FD + "',QOENSWERT,0) AS " + FD.Trim() + ",";
                if (FD != "B050"&& FD!="X050")
                {
                    QT += " ROUND(AVG(NVL((SELECT TRIM(QOEASWERT) AS QOEASWERT FROM QOSTEP Q1 WHERE Q1.QOEMANDANT = Q.QOKMANDANT AND Q1.QOEORDER = Q.QOKORDER AND RTRIM(Q1.QOEMERKM) = '" + FD.Trim() + "'),'0')),2) AS " + FD.Trim() + ",";
                }
                else
                {
                    QT += " MAX(NVL((SELECT TRIM(QOEASWERT)||TRIM(QOEKOMMENT) AS QOEASWERT FROM QOSTEP Q1 WHERE Q1.QOEMANDANT = Q.QOKMANDANT AND Q1.QOEORDER = Q.QOKORDER AND RTRIM(Q1.QOEMERKM) = '" + FD.Trim() + "'),'0')) AS " + FD.Trim() + ",";
                }
            }
            QT += "****";
            QT = QT.Replace(",****", ",");
            if (QT == "****")
                QT = "";
            return QT;
        }
        public string AVGQT()
        {
            QT = "";
            if (QT.Length > 0)
                return QT;
            dwData = TBMain.NewRow();
            dwData["QPMMANDANT"] = app.Mandant;
            SResult = QueryQPMERKM(dwData, -2);
            string FD = "";
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                FD = row["QPMMERKM"].ToString().Trim();
                // QT += "MAX(DECODE(QOEMERKM,'" + row["QPMMERKM"].ToString() + "',QOENSWERT,0)) AS " + row["QPMMERKM"].ToString().Trim() + ",";
                //   QT += "DECODE(RTRIM(QOEMERKM),'" + FD + "',QOENSWERT,0) AS " + FD.Trim() + ",";
                if (FD != "B050" && FD != "X050"&& FD!="S013" && FD!="Y010")
                {
                    QT += " TO_CHAR(ROUND(AVG(" + FD.Trim() + "),2)) AS " + FD.Trim() + ",";
                }
                else
                {
                    QT += " MAX(NVL(" + FD.Trim() + ",'0')) AS " + FD.Trim() + ",";
                }
            }
            QT += "****";
            QT = QT.Replace(",****", ",");
            if (QT == "****")
                QT = "";
            return QT;
        } 
    }
}
